package com.ocnyang.qbox1.app1.module.setting;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.MenuItem;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.ocnyang.qbox1.app1.R;
import com.ocnyang.qbox1.app1.base.BaseCommonActivity;
import com.ocnyang.qbox1.app1.config.Const;
import com.ocnyang.qbox1.app1.db.domain.User;
import com.ocnyang.qbox1.app1.db.service.UserService;
import com.ocnyang.qbox1.app1.module.mains.MainsActivity;
import com.ocnyang.qbox1.app1.utils.SPUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class RegistActivity extends BaseCommonActivity implements CompoundButton.OnCheckedChangeListener {


    UserService uService;
    @BindView(R.id.iv_login_top)
    ImageView ivLoginTop;
    @BindView(R.id.iv_common_close)
    ImageView ivCommonClose;
    @BindView(R.id.phone_et)
    EditText phoneEt;
    @BindView(R.id.pass_et)
    EditText passEt;
    @BindView(R.id.parent_psd)
    RelativeLayout parentPsd;
    @BindView(R.id.pass_et1)
    EditText passEt1;
    @BindView(R.id.parent_psd1)
    RelativeLayout parentPsd1;
    @BindView(R.id.button)
    TextView button;


    @Override
    public void initContentView() {
        setContentView(R.layout.activity_regist);

    }

    @Override
    public void initView() {
        uService = new UserService(RegistActivity.this);
        initSwitch();

    }


    private void initSwitch() {
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void initPresenter() {

    }


    @Override
    public void onCheckedChanged(CompoundButton compoundButton, boolean b) {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.iv_common_close, R.id.button})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_common_close:
                finish();
                break;
            case R.id.button:
                String name = phoneEt.getText().toString().trim();
                String pass = passEt.getText().toString().trim();
                String pass1 = passEt1.getText().toString().trim();

                if (TextUtils.isEmpty(name)){
                    Toast.makeText(RegistActivity.this, "手机号码不能为空", Toast.LENGTH_LONG).show();
                    return;
                }
                if (TextUtils.isEmpty(pass)){
                    Toast.makeText(RegistActivity.this, "密码不能为空", Toast.LENGTH_LONG).show();
                    return;
                }
                if (TextUtils.isEmpty(pass1)){
                    Toast.makeText(RegistActivity.this, "密码不能为空", Toast.LENGTH_LONG).show();
                    return;
                }
                if (!pass.equals(pass1)){
                    Toast.makeText(RegistActivity.this, "两次密码输入不一致", Toast.LENGTH_LONG).show();
                    return;
                }
                Boolean is = uService.yanzheng(name);
                if (is) {
                    Toast.makeText(RegistActivity.this, "此号码已注册过", Toast.LENGTH_LONG).show();
                } else {
                    User user = new User();
                    user.setUsername(name);
                    user.setPassword(pass);
                    uService.register(user);
                    SPUtils.put(RegistActivity.this,Const.TOKEN,"已登录");
                    Toast.makeText(RegistActivity.this, "注册成功", Toast.LENGTH_LONG).show();
                    startActivity(new Intent(RegistActivity.this,MainsActivity.class));
                    finish();
                }
                break;
        }
    }
}
